package cn.wyu.dao;

import cn.wyu.pojo.User;
import cn.wyu.util.JDBCUtil;

import java.sql.ResultSet;
import java.sql.SQLException;

public class userDaoimpl implements userDao {

    @Override
    public Boolean adduser(User user) {
        System.out.println("userDaoImpl执行");
        String sql = "insert into ec_user (login_name,password,name,sex,phone,address) VALUES(?,?,?,?,?,?);";

        String loginName = user.getLoginName();
        String password = user.getPassword();
        String realName = user.getRealName();
        int sex = user.getSex();
        String phone = user.getPhone();
        String addres = user.getAddres();
        String userstr = findUser(user);
        int result = 0;
        if (userstr == null) {
            result = JDBCUtil.executeUpdate(sql, loginName, password, realName, sex, phone, addres);
            System.out.println("result:" + result);
        }
        //表明插入成功
        if (result > 0) {
            System.out.println("插入成功,result="+result);
            return true;
        }
        System.out.println("result="+result);
        return false;
    }

    @Override
    public String findUser(User user) {
        String sql = "select login_name from ec_user where login_name = ?";
        String userName = null;
        String loginName = user.getLoginName();
        ResultSet resultSet = null;
        resultSet = JDBCUtil.executeQuery(sql, loginName);

        if(resultSet==null) return  null;
        try {
            while (resultSet.next()){
                if(resultSet==null);
                else userName = resultSet.getString("login_name");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("userName:" + userName);
        return userName;
    }

    @Override
    public String getPassword(User user) {
        String sql = "select password from ec_user where login_name = ?";
        String password = null;
        ResultSet resultSet = JDBCUtil.executeQuery(sql, user.getLoginName());

        try {
            while (resultSet.next()) {
                password = resultSet.getString("password");
                System.out.println("密码是:" + password);
            }
            return password;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return password;
    }
}
